﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.Model;
using MyBlog.DataAccess;
using System.Linq.Expressions;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;

namespace MyBlog.Business
{
    public class BusinessAlbum :IBusinessAlbum
    {
        IDALAlbum _dalAlbum;
        IUnitOfWork _unitWork;
        public BusinessAlbum(IDALAlbum dalAlbum, IUnitOfWork unitWork)
        {
            _dalAlbum = dalAlbum;
            _unitWork = unitWork;
        }
        public void Add(AlbumModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Album>(model);
            _dalAlbum.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(AlbumModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Album>(model);
            _dalAlbum.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<AlbumModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Album>>(models);
            _dalAlbum.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<AlbumModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Album>>(models);
            _dalAlbum.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<AlbumModel> GetAll(bool isTracking = true)
        {
            var entities = _dalAlbum.GetAll(isTracking);
            return Mapper.Map<List<AlbumModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalAlbum.Delete(primaryKey);
            _unitWork.Commit();
        }

        public AlbumModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalAlbum.Get(primaryKey);
            return Mapper.Map<AlbumModel>(dataEntity);
        }

        public void Delete(AlbumModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Album>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalAlbum.Delete(where);
            _unitWork.Commit();
        }

        public AlbumModel Get(AlbumModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Album>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            var dataEntity = _dalAlbum.Get(where);
            return Mapper.Map<AlbumModel>(dataEntity);
        }

        public List<AlbumModel> GetAll(AlbumModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Album>();
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            var entities = _dalAlbum.GetAll(where, isTracking);
            return Mapper.Map<List<AlbumModel>>(entities);
        }

        public List<AlbumModel> GetAll(AlbumModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
